home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / crobots.arc / SNIPER.R < prev   
Text File  |  1985-12-16  |  5KB  |  177 lines

  1.  
  2. /* sniper */
  3. /* strategy: since a scan of the entire battlefield can be done in 90 */
  4. /* degrees from a corner, sniper can scan the field quickly. */
  5.  
  6. /* external variables, that can be used by any function */
  7. int corner;           /* current corner 0, 1, 2, or 2 */
  8. int c1x, c1y;         /* corner 1 x and y */
  9. int c2x, c2y;         /*   "    2 "  "  " */
  10. int c3x, c3y;         /*   "    3 "  "  " */
  11. int c4x, c4y;         /*   "    4 "  "  " */
  12. int s1, s2, s3, s4;   /* starting scan position for corner 1 - 4 */
  13. int sc;               /* current scan start */
  14. int d;                /* last damage check */
  15.  
  16. /* main */
  17. main()
  18. {
  19.   int closest;        /* check for targets in range */
  20.   int range;          /* range to target */
  21.   int dir;            /* scan direction */
  22.  
  23.   /* initialize the corner info */
  24.   /* x and y location of a corner, and starting scan degree */
  25.   c1x = 10;  c1y = 10;  s1 = 0;
  26.   c2x = 10;  c2y = 990; s2 = 270;
  27.   c3x = 990; c3y = 990; s3 = 180;
  28.   c4x = 990; c4y = 10;  s4 = 90;
  29.   closest = 9999;
  30.   new_corner();       /* start at a random corner */
  31.   d = damage();       /* get current damage */
  32.   dir = sc;           /* starting scan direction */
  33.  
  34.   while (1) {         /* loop is executed forever */
  35.  
  36.     while (dir < sc + 90) {  /* scan through 90 degree range */
  37.       range = scan(dir,1);   /* look at a direction */
  38.       if (range <= 700 && range > 0) {
  39.         while (range > 0) {    /* keep firing while in range */
  40.           closest = range;     /* set closest flag */
  41.           cannon(dir,range);   /* fire! */
  42.           range = scan(dir,1); /* check target again */
  43.           if (d + 15 > damage())  /* sustained several hits, */
  44.             range = 0;            /* goto new corner */
  45.         }
  46.         dir -= 10;             /* back up scan, in case */
  47.       }
  48.  
  49.       dir += 2;                /* increment scan */
  50.       if (d != damage()) {     /* check for damage incurred */
  51.         new_corner();          /* we're hit, move now */
  52.         d = damage();
  53.         dir = sc;
  54.       }
  55.     }
  56.  
  57.     if (closest == 9999) {       /* check for any targets in range */
  58.       new_corner();             /* nothing, move to new corner */
  59.       d = damage();
  60.       dir = sc;
  61.     } else                      /* targets in range, resume */
  62.       dir = sc;
  63.     closest = 9999;
  64.   }
  65.  
  66. }  /* end of main */
  67.  
  68. /* new corner function to move to a different corner */
  69. new_corner() {
  70.   int x, y;
  71.   int angle;
  72.   int new;
  73.  
  74.   new = rand(4);           /* pick a random corner */
  75.   if (new == corner)       /* but make it different than the */
  76.     corner = (new + 1) % 4;/* current corner */
  77.   else
  78.     corner = new;
  79.   if (corner == 0) {       /* set new x,y and scan start */
  80.     x = c1x;
  81.     y = c1y;
  82.     sc = s1;
  83.   }
  84.   if (corner == 1) {
  85.     x = c2x;
  86.     y = c2y;
  87.     sc = s2;
  88.   }
  89.   if (corner == 2) {
  90.     x = c3x;
  91.     y = c3y;
  92.     sc = s3;
  93.   }
  94.   if (corner == 3) {
  95.     x = c4x;
  96.     y = c4y;
  97.     sc = s4;
  98.   }
  99.  
  100.   /* find the heading we need to get to the desired corner */
  101.   angle = plot_course(x,y);
  102.  
  103.   /* start drive train, full speed */
  104.   drive(angle,100);
  105.  
  106.   /* keep traveling until we are within 100 meters */
  107.   /* speed is checked in case we run into wall, other robot */
  108.   /* not terribly great, since were are doing nothing while moving */
  109.  
  110.   while (distance(loc_x(),loc_y(),x,y) > 100 && speed() > 0)
  111.     ;
  112.  
  113.   /* cut speed, and creep the rest of the way */
  114.  
  115.   drive(angle,20);
  116.   while (distance(loc_x(),loc_y(),x,y) > 10 && speed() > 0)
  117.     ;
  118.  
  119.   /* stop drive, should coast in the rest of the way */
  120.   drive(angle,0);
  121. }  /* end of new_corner */
  122.  
  123. /* classical pythagorean distance formula */
  124. distance(x1,y1,x2,y2)
  125. int x1;
  126. int y1;
  127. int x2;
  128. int y2;
  129. {
  130.   int x, y;
  131.  
  132.   x = x1 - x2;
  133.   y = y1 - y2;
  134.   d = sqrt((x*x) + (y*y));
  135.   return(d);
  136. }
  137.  
  138. /* plot course function, return degree heading to */
  139. /* reach destination x, y; uses atan() trig function */
  140. plot_course(xx,yy)
  141. int xx, yy;
  142. {
  143.   int d;
  144.   int x,y;
  145.   int scale;
  146.   int curx, cury;
  147.  
  148.   scale = 100000;  /* scale for trig functions */
  149.   curx = loc_x();  /* get current location */
  150.   cury = loc_y();
  151.   x = curx - xx;
  152.   y = cury - yy;
  153.  
  154.   /* atan only returns -90 to +90, so figure out how to use */
  155.   /* the atan() value */
  156.  
  157.   if (x == 0) {      /* x is zero, we either move due north or south */
  158.     if (yy > cury)
  159.       d = 90;        /* north */
  160.     else
  161.       d = 270;       /* south */
  162.   } else {
  163.     if (yy < cury) {
  164.       if (xx > curx)
  165.         d = 360 + atan((scale * y) / x);  /* south-east, quadrant 4 */
  166.       else
  167.         d = 180 + atan((scale * y) / x);  /* south-west, quadrant 3 */
  168.     } else {
  169.       if (xx > curx)
  170.         d = atan((scale * y) / x);        /* north-east, quadrant 1 */
  171.       else
  172.         d = 180 + atan((scale * y) / x);  /* north-west, quadrant 2 */
  173.     }
  174.   }
  175.   return (d);
  176. }
  177.